<template>
  <div class="app-container">
    <div class="bg-container">
      <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams">
        <el-form-item label="指标名称">
          <el-select style="width: 280px;" v-model="queryParams.indicatorName" placeholder="请选择指标名称">
            <el-option v-for="item in indicatorList" :key="item.id" :label="item.title" :value="item.title"></el-option>
          </el-select> 
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              icon="Plus"
              plain
              type="primary"
              @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
              :disabled="multiple"
              icon="Delete"
              plain
              type="danger"
              @click="handleDeleteList"
          >删除
          </el-button>
        </el-col> -->
      </el-row>
      <el-table v-loading="loading" :data="computerList" :show-overflow-tooltip="true" :stripe="true"
                @selection-change="handleSelectionChange" height="400px">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column label="指标名称" align="center" prop="indicatorName"/>
        <el-table-column label="描述" show-overflow-tooltip align="center" prop="description"/>
         <el-table-column label="是否显示" align="center" prop="isVisible">
           <template #default="scope">
             <span>{{ scope.row.isVisible ? '显示' : '不显示' }}</span>
           </template>
         </el-table-column>
         <el-table-column label="显示形式" align="center" prop="displayType">
           <template #default="scope">
             <el-tag :type="scope.row.displayType === 0 ? 'primary' : 'success'">{{scope.row.displayType === 0 ? '折线图' : '饼图'}}</el-tag>
           </template>
         </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作"
                         width="140px">
          <template #default="scope">
            <el-button circle icon="Edit" plain type="primary"
                       @click="handleUpdate(scope.row)">
            </el-button>
            <el-button circle icon="Delete" plain type="danger"
                       @click="handleDelete(scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:current-page="queryParams.pageNo"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[3, 5, 7, 10]"
          :page-size="10"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; justify-content: flex-end"
          @size-change="getList"
          @current-change="getList"
      />
    </div>

    <el-dialog v-model="open" :title="title" center append-to-body width="600px">
      <el-form ref="computerRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="指标名称" prop="indicatorName">
          <el-select v-model="form.indicatorName" placeholder="请选择指标名称">
            <el-option v-for="item in indicatorList" :key="item.id" :label="item.title" :value="item.title"></el-option>
          </el-select> 
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入指标描述"/>
        </el-form-item>
        <el-form-item label="显示形式" prop="displayType">
          <el-radio-group v-model="form.displayType">
            <el-radio :value="0">折线图</el-radio>
            <el-radio :value="1" v-if="indicatorList.some(item => item.CircleChart &&item.title === form.indicatorName)">饼图</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否显示" prop="isVisible">
          <el-switch v-model="form.isVisible"/>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script name="DeployHost" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import { getIndicatorList, deleteIndicator, updateIndicator, createIndicator } from "@/api/report.js";
import { indicatorList } from './indicator'

const {proxy} = getCurrentInstance();

const computerList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const initForm = {
  id: null,
  indicatorName: null,
  indicatorId: null,
  description: null,
  isVisible: true,
  displayType: 1
}
const form = reactive({ ...initForm });


const data = reactive({
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    indicatorName: null
  },
  rules: {
    indicatorName: [
      {required: true, message: "请输入指标名称", trigger: "blur"}
    ],
    description: [
      {required: true, message: "请输入指标描述", trigger: "blur"}
    ],
    displayType: [
      {required: true, message: "请选择显示形式", trigger: "blur"}
    ],
    isVisible: [
      {required: true, message: "请选择是否显示", trigger: "blur"}
    ]
  }
});

const {queryParams, rules} = toRefs(data);

// 获取指标列表
function getList() {
  loading.value = true;
  getIndicatorList(queryParams.value).then(response => {
    computerList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  Object.assign(form, { ...initForm });
  proxy.resetForm("computerRef");
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.indicatorName = null;
  getList();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 新增按钮操作
function handleAdd() {
  // reset();
  form.id = null;
  form.indicatorName = null;
  form.indicatorId = null;
  form.description = null;
  form.isVisible = true;
  form.displayType = 0;
  open.value = true;
  title.value = "添加指标";
}

// 修改按钮操作
function handleUpdate(row) {
  Object.assign(form, { ...row });
  open.value = true;
  title.value = "修改指标";
}

// 提交按钮
function submitForm() {
  proxy.$refs["computerRef"].validate(valid => {
    if (valid) {
      if (form.id != null) {
        updateIndicator(form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        // 如果新增的指标 存在与table中，则不允许新增
        if (computerList.value.some(item => item.indicatorName === form.indicatorName)) {
          proxy.$modal.msgError("指标名称已存在");
          return;
        }
        form.indicatorId = indicatorList.find(item => item.title === form.indicatorName).id;
        createIndicator(form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 删除按钮操作
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除指标"' + row.title + '"的数据项？').then(function () {
    return deleteIndicator(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}


getList();
</script>